#include "linInputStream.h"
#include "core/Core.h"

#include <fstream>

using namespace std;
using namespace neiderra::core;

linInputStream::linInputStream(Core* core): iInputStream(core)
{
	//ready = false;
	_input = new ifstream();
}

bool linInputStream::open( const char* filename )
{
	if(_input->is_open())
		_input->close();
	_filename = filename;
	_input->open(filename);
	return _input->is_open();
	//ready = _input.open
}

bool linInputStream::reopen( )
{
	close();
	return open(_filename.c_str());
}

void linInputStream::close()
{
	_input->close();
}

int linInputStream::size()
{
	long cpos = pos();
	seek( 0, stEnd );
	int size = pos();
	seek( cpos, stBegin );
	return size;
}

long linInputStream::pos()
{
	return _input->tellg();
}

int	linInputStream::read(char* buf, int n)
{
	_input->read(buf, n);
	return 1;
}

int linInputStream::seek( long n, SeekType st)
{
	switch(st)
	{
		case stBegin:
			_input->seekg(n, ios_base::beg);
			break;
		case stEnd:
			_input->seekg(n, ios_base::end);
			break;
		case stCurrent:
			_input->seekg(n, ios_base::cur);
			break;
		default:
			break;
	}
}

const char* linInputStream::sourceName() {return _filename.c_str(); }

linInputStream::~linInputStream()
{
	delete _input;
}
